using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ghasam.Business.Services;
using Ghasam.Business.Domain.Entity;
using System.Collections.Generic;
using Mani.Common;

public partial class SearchStockHistory : System.Web.UI.Page
{
    #region "Private Property"
    private IStockService stockService;
    private int _status;
    private string _name = null;
    private string _serial = null;
    private string _binNumber = null;
    private int _stockId;
    private int _qty;
    private DateTime? _exitDate = null;
    private DateTime? _returnDate = null;
    private DateTime? _modifyDate = null;
    private int _empID;
    private string _usageDesc = null;
    private string _desc = null;
    DataTable dt = new DataTable();
    #endregion
    #region "Public Method"
    public IStockService StockService
    {
        get { return stockService; }
        set { stockService = value; }
    }
    
    #endregion
    #region "Private Method"
    private void getSearchControlsValue()
    {
        if (!String.IsNullOrEmpty(txtExitDate.Text))
            _exitDate = MyPersianDate.PersianToDateTime(txtExitDate.Text);

        if (!String.IsNullOrEmpty(txtReturnDate.Text))
            _returnDate = MyPersianDate.PersianToDateTime(txtReturnDate.Text);

        _name = txtName.Text;
        _serial = txtSerial.Text;
        _binNumber = txtBinNumber.Text;
       if (!String.IsNullOrEmpty(txtEmpID.Text))
        _empID = Convert.ToInt32(txtEmpID.Text);
       _status = Convert.ToInt32(ddlStatus.SelectedItem.Value);
    }
    private void getControlsValueForUpdate()
    {
        _returnDate = DateTime.Today.Date;
        _modifyDate = DateTime.Today.Date;
        _stockId =Convert.ToInt32(lblGoodID.Text);
        _binNumber = txtRGoodBinNo.Text;
        _desc = txtRGoodDesc.Text;
        _empID = Convert.ToInt32(txtREmpId.Text);
        _usageDesc = txtRGoodUsage.Text;
        _qty =Convert.ToInt32(lblQty.Text);
        _status = Convert.ToInt32(ddlStockStatus.SelectedItem.Value); 
    }
    private void setResultControls(string rowIndx)
    {
        int indx = Convert.ToInt32(rowIndx);
        lblGoodID.Text = grdStockHistory.Rows[indx].Cells[0].Text;
        txtRGoodName.Text = grdStockHistory.Rows[indx].Cells[1].Text;
        txtRGoodBinNo.Text = grdStockHistory.Rows[indx].Cells[2].Text;
        txtRGoodSerial.Text = grdStockHistory.Rows[indx].Cells[3].Text;
        txtREmpId.Text = grdStockHistory.Rows[indx].Cells[4].Text;
        txtRGoodUsage.Text = grdStockHistory.Rows[indx].Cells[8].Text;
        txtRGoodDesc.Text = grdStockHistory.Rows[indx].Cells[9].Text;
        dt = (DataTable)ViewState["dt"];
        lblQty.Text = dt.Rows[indx].ItemArray[9].ToString();
    }
    private void bindGrid()
    {
        
        DataColumn key = new DataColumn();
        DataColumn dc = new DataColumn();
        DataColumn dc1 = new DataColumn();
        DataColumn dc2 = new DataColumn();
        DataColumn dc3 = new DataColumn();
        DataColumn dc4 = new DataColumn();
        DataColumn dc5 = new DataColumn();
        DataColumn dc6 = new DataColumn();
        DataColumn dc7 = new DataColumn();
        DataColumn dc8 = new DataColumn();
        DataColumn dc9 = new DataColumn();

        dc.ColumnName = "name";
        dc1.ColumnName = "binNumber";
        dc2.ColumnName = "serial";
        dc3.ColumnName = "empId";
        dc7.ColumnName = "exitDate";
        dc4.ColumnName = "returnDate";
        dc5.ColumnName = "usage";
        dc6.ColumnName = "desc";
        key.ColumnName = "Id";
        dc8.ColumnName = "qty";
        dc9.ColumnName = "status";

        dt.Columns.Add(dc);
        dt.Columns.Add(dc1);
        dt.Columns.Add(dc2);
        dt.Columns.Add(dc3);
        dt.Columns.Add(dc4);
        dt.Columns.Add(dc5);
        dt.Columns.Add(dc6);
        dt.Columns.Add(dc7);
        dt.Columns.Add(key);
        dt.Columns.Add(dc8);
        dt.Columns.Add(dc9);

        DataRow dr = null;
        int rowcnt = 0;
        IList<StockHistory> list = StockService.FindStockHistory(_name, _binNumber, _serial, _status, _empID, _exitDate, _returnDate);
        foreach (StockHistory stockList in list)
        {
            dr = dt.NewRow();
            dr[0] = stockList.Stock.StockName;
            dr[1] = stockList.Stock.BinNumber;
            dr[2] = stockList.Stock.Serial;
            dr[3] = stockList.EmployeeID;
            dr[7] = MyPersianDate.DateTimeToPersian(stockList.ExitDate);
            dr[4] = MyPersianDate.DateTimeToPersian(stockList.ReturnDate);
            dr[5] = stockList.UsageDescription;
            dr[6] = stockList.Description;
            dr[8] = stockList.Stock.Id;
            dr[9] = stockList.Stock.Quantity;
            dr[10] = setStatusName(stockList.StockStatus.Id.ToString());
            rowcnt+=1;
            dt.Rows.Add(dr);
        }
        ViewState["dt"] = dt;
        grdStockHistory.DataSource = dt;
        grdStockHistory.DataBind();
        lblRowCount.Text =""+ rowcnt;
    }
    private void updateStockHistory()
    {
        getControlsValueForUpdate();
        StockService.StockHistorySaveOrUpadate(_stockId,_empID,_status,_exitDate,_returnDate,_usageDesc
        ,_desc,_modifyDate);
        StockService.StockUpdate(_stockId, _qty + 1);

    }
    private void fillStockStateList()
    {
        IList<StockStatus> status = StockService.FindAllState();

        ddlStockStatus.DataSource = status;
        ddlStockStatus.DataValueField = "ID";
        ddlStockStatus.DataTextField = "StatusDesc";
        ddlStockStatus.DataBind();

        ddlStatus.DataSource = status;
        ddlStatus.DataValueField = "ID";
        ddlStatus.DataTextField = "StatusDesc";
        ddlStatus.DataBind();
    }
    private String setStatusName(string id)
    {
        foreach (ListItem listItem in ddlStatus.Items)
        {
            if (listItem.Value == id)
                return listItem.Text;
        }

        return"";
    }
    private ListItem listItem()
    {
        ListItem li = new ListItem();
        li.Text = "---";
        li.Value = "0";
        return li;
    }
    #endregion
    #region "Event"
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            fillStockStateList();
        }

    }
    protected void grdStockHistory_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        setResultControls(e.CommandArgument.ToString());
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            getSearchControlsValue();
            bindGrid();

        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        updateStockHistory();
    }
    protected void ddlStatus_DataBound(object sender, EventArgs e)
    {
        ddlStatus.Items.Insert(0, listItem());
    }
    #endregion
}
